/*
 * Copyright (c) GoKeep by gokeep.org. 2022-2023. All rights reserved
 */

package org.gokeep.application.config;

import com.amazonaws.ClientConfiguration;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.gokeep.common.config.properties.StrongProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * AmazonS3 配置类，配置S3客户端
 * 支持：s3，minio, oss, 七牛云
 * @author xuning
 */
@Slf4j
@Configuration
public class AmazonS3Config {
    @Resource
    private StrongProperties strongProperties;
    public static final String SIGNER_OVERRIDE = "AWSS3V4SignerType";

    /**
     * 构建AmazonS3存储协议客户端
     * @return
     */
    @Bean
    public AmazonS3 amazonS3() {
        AWSCredentials credentials = new BasicAWSCredentials(strongProperties.getAccessKey(), strongProperties.getSecretKey());
        ClientConfiguration clientConfiguration = new ClientConfiguration();
        clientConfiguration.setSignerOverride(SIGNER_OVERRIDE);
        return AmazonS3ClientBuilder.standard()
                .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(strongProperties.getEndpoint(), Regions.CN_NORTH_1.name()))
                .withPathStyleAccessEnabled(true)
                .withClientConfiguration(clientConfiguration)
                .withCredentials(new AWSStaticCredentialsProvider(credentials))
                .build();
    }
}
